package com.company.management.controller;
import com.company.management.dto.*;
import com.company.management.modal.entity.User;
import com.company.management.result.Code;
import com.company.management.result.Result;
import com.company.management.service.report.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/api/task")
@Slf4j
public class TaskController {

    @Autowired
    private TaskService taskService;
    @Value("${spring.location.dir}")
    private String uploadDir;

    /**
     *创建任务
     */
    @PostMapping("/createtask")
    public Result createtask(HttpServletRequest request, @RequestBody CreateTaskReq req) {

        Integer userId = (Integer) request.getAttribute("user_id");
        if (null == userId) {
            return new Result(Code.statusCode_401, "未登录或登录状态已超时");
        }
        req.setUserId(userId);
        if (Objects.isNull(req)) {
            return new Result(Code.statusCode_406, "新增任务任务内容不能为空");
        }
        if (CollectionUtils.isEmpty(req.getList())) {
            return new Result(Code.statusCode_406, "新增任务必须创建一条记录");

        }
        return taskService.createTask(req);
    }
    /**
     * 修改任务
     * @param req
     * @return
     */
    @PostMapping("/updatetask")
    public Result updatetask(HttpServletRequest request, @RequestBody UpdateTaskReq req){
//        Integer userId = (Integer) request.getAttribute("user_id");
//        if (null == userId) {
//            return new Result(Code.statusCode_401, "未登录或登录状态已超时");
//        }
//        req.setUserId(userId);
        if (Objects.isNull(req)) {
            return new Result(Code.statusCode_406, "修改任务不能为空");
        }
        if (CollectionUtils.isEmpty(req.getList())) {
            return new Result(Code.statusCode_406, "修改任务必须创建一条记录");
        }
        return taskService.updateTask(req);
    }

    /**
     * 查询任务
     * @return
     */
    @PostMapping("/gettask")
    public Result gettask(HttpServletRequest request, @RequestBody User user) {
//        Integer userId = (Integer) request.getAttribute("user_id");
//        if (null == userId) {
//            return new Result(Code.statusCode_401, "未登录或登录状态已超时");
//        }
        return taskService.selectTaskList(user);
    }

    /**
     * 删除任务
     * @return
     */
    @PostMapping("/deletetask")
    public Result deletetask(HttpServletRequest request,@RequestBody TaskDeleReq req) {
//        Integer userId = (Integer) request.getAttribute("user_id");
//        if (null == userId) {
//            return new Result(Code.statusCode_401, "未登录或登录状态已超时");
//        }
        if (Objects.isNull(req)) {
            return new Result(Code.statusCode_406, "删除任务id不能为空");
        }
        return taskService.deleteTask(req);
    }

    /**
     * 查询详细任务
     * @return
     */
    @PostMapping("/querytaskdetail")
    public Result querytaskdetail(@RequestBody QueryTaskDetailReq req){
        if (Objects.isNull(req)) {
            return new Result(Code.statusCode_406, "查询任务详情Id不能为空");
        }
        return taskService.queryTaskDetail(req);
    }

    /**
     *创建任务步骤
     */
    @PostMapping("/createstep")
    public Result createstep(@RequestBody CreateStepReq req) {
        if (Objects.isNull(req)) {
            return new Result(Code.statusCode_406, "创建任务不能为空");
        }

        return taskService.createStep(req);
    }

    /**
     * 修改任务步骤
     * @return
     */
    @PostMapping("/updatestep")
    public Result updatestep(@RequestBody UpdateStepReq req){
        if (Objects.isNull(req)) {
            return new Result(Code.statusCode_406, "修改任务不能为空");
        }


        if (CollectionUtils.isEmpty(req.getList())) {
            return new Result(Code.statusCode_406, "修改任务必须创建一条记录");

        }
        return taskService.updateStep(req);
    }

    /**
     * 查询任务步骤
     * @return
     */
    @PostMapping("/querystepdetail")
    public Result querystepdetail(@RequestBody QueryStepDetailReq req){
        if (Objects.isNull(req)) {
            return new Result(Code.statusCode_406, "查询任务步骤不能为空");
        }
        return taskService.queryStepDetail(req);
    }

    /**
     * 删除任务步骤
     * @return
     */
    @PostMapping("/deletestep")
    public Result delete(@RequestBody StepDeleReq req){
        if (Objects.isNull(req)) {
            return new Result(Code.statusCode_406, "修改任务步骤不能为空");
        }
        return taskService.deleteStep(req);
    }


    /**
     * file
     * @return
     */
    @PostMapping("/upload")
    public Result uploadFile(@RequestBody fileInfoReq req){

        return taskService.uploadFile(req,uploadDir);
    }

    /**
     * @return
     */
    @PostMapping("/getuserlist")
    public Result getuserlist(){

        return taskService.getuserlist();
    }

    /**
     * @return
     */
    @PostMapping("/assigntask")
    public Result assigntask(HttpServletRequest request,@RequestBody AssignTaskReq req){
//        Integer userId = (Integer) request.getAttribute("user_id");
        if (Objects.isNull(req)) {
            return new Result(Code.statusCode_406, "分配任务请求参数体");
        }
        if(null== req.getUserId()){
            return new Result(Code.statusCode_401, "分配任务请求参数分配人");
        }
        if (StringUtils.isEmpty(req.getUserIds())) {
            return new Result(Code.statusCode_406, "分配任务分配人不能为空");
        }
        return taskService.assigntask(req);
    }

    /**
     * @return
     */
    @PostMapping("/uploadFile")
    public Result uploadFile( @RequestParam("file") MultipartFile file,@RequestParam("taskId") String taskId,@RequestParam("userId") Integer userId){
//        Integer userId = (Integer) request.getAttribute("user_id");

        return taskService.uploadFile(file,taskId,userId,uploadDir);
    }


//    /**
//     * test
//     * @return
//     */
//    @GetMapping("/getuserinfo")
//    public Result getuserinfo(){
//
//        return taskService.selectTaskList();
//    }

}
